<template>
  <div class="main-content">
    <div class="search between">
      <div class="query">
        <el-input style="width: 180px;" placeholder="会员手机号" v-model="params.phone" clearable
          @change="getTable"></el-input>
        <el-select style="width: 180px;" v-model="params.state" placeholder="订单状态" clearable @change="getTable">
          <el-option label="待支付" value="1" />
          <el-option label="已支付" value="2" />
          <el-option label="已取消" value="3" />
          <el-option label="待退款" value="4" />
          <el-option label="退款成功" value="5" />
          <el-option label="拒绝退款" value="6" />
        </el-select>
        <span class="fold" v-show="isFold">
        </span>
        <!-- <el-button class="ico-button" @click="switchFold" :icon="isFold ? 'ArrowUp' : 'ArrowDown'"></el-button> -->
      </div>
      <div class="set-group">
        <!-- <el-button @click="add" type="primary">新增</el-button> -->
        <el-button class="ico-button" @click="refresh">
          <el-icon>
            <Refresh />
          </el-icon>
        </el-button>
      </div>
    </div>
    <div class="table">
      <div class="table-main">
        <el-table :data="table.data" resizable border show-overflow-tooltip max-height="100%" height="100%"
          v-loading="table.load">
          <template v-for="row in table.column">
            <el-table-column :label="row.label" :prop="row.key" :width="row.width" :align="row.align"
              :fixed="row.fixed">
              <template #default="scope">
                <span v-if="row.key == 'member_avatar'">
                  <img :src="$imgUrl + scope.row[row.key]" style="width:50px;height:50px" alt="">
                </span>
                <span v-else>
                  {{ scope.row[row.key] }}
                </span>
              </template>
            </el-table-column>
          </template>
          <el-table-column label="相关操作" column-key="handle" width="180" align="center" fixed="right">
            <template #default="scope">
              <span v-if="scope.row.state == 4">
                <el-button-group>
                  <el-popconfirm popper-class="limit-popper" title="确定要同意退款吗?" @confirm="review(scope.row.order_sn, 1)">
                    <template #reference>
                      <el-button link type="success">同意退款</el-button>
                    </template>
                  </el-popconfirm>
                  <el-popconfirm popper-class="limit-popper" title="确定要拒绝同意吗?" @confirm="review(scope.row.order_sn, 2)">
                    <template #reference>
                      <el-button link type="danger">拒绝退款</el-button>
                    </template>
                  </el-popconfirm>
                </el-button-group>
              </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page">
        <el-pagination v-model:current-page="pages.page" v-model:page-size="pages.page_size"
          :page-sizes="[30, 50, 100, 200, 300, 500]" :small="true" :background="true"
          layout="total, sizes, prev, pager, next, jumper" :total="pages.count" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>
<script setup name="RechargeList">

const { proxy } = getCurrentInstance()

// 搜索表单 下拉收起 start 
const isFold = ref(false)
function switchFold() {
  isFold.value = !isFold.value
}
// 搜索表单 下拉收起 end

// 表格字段配置 start
const table = reactive({
  load: false,
  column: [
    { label: '订单号', key: 'order_sn', width: '150', show: true, align: 'center' },
    { label: '会员', key: 'phone', width: 'auto', show: true, align: 'center' },
    { label: '订单状态', key: 'state_name', width: 'auto', show: true, align: 'center' },
    { label: '充值金额', key: 'amount', width: 'auto', show: true, align: 'center' },
    { label: '支付金额', key: 'payment_amount', width: 'auto', show: true, align: 'center' },
    { label: '支付时间', key: 'pay_time', width: '150', show: true, align: 'center' },
    { label: '下单时间', key: 'create_time', width: '150', show: true, align: 'center' },
  ],
  data: []
})
// 表格字段配置 end 

onMounted(() => {
  getTable()
})

// 页面数据 start
const params = reactive({
  name: '',
})
const pages = reactive({
  page: 1,
  page_size: 30,
  count: 0
})

function handleSizeChange(val) {
  pages.page_size = val
  getTable()
}
function handleCurrentChange(val) {
  pages.page = val
  getTable()
}
function refresh() {
  pages.page = 1
  getTable()
}

async function review(order_sn, type) {
  let res = await proxy.$post("RechargeRechargeReview", {
    order_sn: order_sn,
    type: type,
  });
  if (res.code === 0) {
    ElMessage({
      type: "success",
      message: "操作成功",
    });
    getTable();
  }
}

async function getTable() {
  table.load = true
  let res = await proxy.$get('RechargeRechargeList', {
    page: pages.page,
    page_size: pages.page_size,
    ...params
  }, false)
  table.load = false
  if (res.code === 0) {
    table.data = res.data.list || []
    pages.count = res.data.total ? Number(res.data.total) : 0
  }
}

// 增删改 end
</script>
<style scoped>
.read-the-docs {
  color: #888;
}
</style>
